package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Transactional
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //首先一个查询操作检查菜品或者套餐是否加入过

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryList(shoppingCart);

        //如果加入过，更新原来那条数据
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumById(shoppingCart);
        } else {
            //如果新添加就插入新的数据，插入新的数据需要查询对应的价格等数据
            if (shoppingCart.getSetmealId() != null) {
                SetmealVO setmealVO = setmealMapper.queryById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                Dish dish = dishMapper.queryById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    /**
     * 用户id查购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> queryList() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        //查询购物车
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryList(shoppingCart);
        return shoppingCarts;
    }

    /**
     * 根据用户id清空购物车
     *
     * @return
     */
    @Override
    public void clean() {
        Long UserId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(UserId);
        //删除全部
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 根据菜品或套餐id删除对应的数据
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //将数据封装到shoppingCart中查询
        Long UserId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(UserId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryList(shoppingCart);

        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            ShoppingCart cart = shoppingCarts.get(0);
            if (cart.getNumber() > 1) {
                //如果该商品存在并且数量大于一，减一
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumById(cart);
            } else {
                //如果小于一，删除一条
                shoppingCartMapper.delete(cart);
            }
        }
    }
}
